<template>
  <el-dialog
    :title="this.isNew ? '新增' : '修改'"
    @closed="dialogClose"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :rules="dataRule" :model="dataForm">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="dataForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="分类图片" prop="imageUrl">
        <single-upload v-model="dataForm.imageUrl"></single-upload>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input v-model.number="dataForm.sort"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitData">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryApi from "@/api/product/category";
import SingleUpload from "@/components/upload/singleUpload";

export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        parentId: 0,
        level: 0,
        name: "",
        sort: 0,
        imageUrl: null,
      },
      isNew: true,
      dataRule: {
        name: [{ required: true, message: "分类名不能为空", trigger: "blur" }],
        imageUrl: [
          { required: true, message: "分类图片不能为空", trigger: "blur" },
        ],
        sort: [
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("排序字段必须填写"));
              } else if (!Number.isInteger(value) || value < 0) {
                console.log(Number.isInteger(value));
                callback(new Error("排序必须是一个大于等于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //新建分类
    initAdd(id) {
      this.isNew = true;
      this.visible = true;
      if (id) {
        this.dataForm.parentId = id;
        this.dataForm.level = 1;
      } else {
        this.dataForm.parentId = 0;
        this.dataForm.level = 0;
      }
    },
    //更新
    initUpdate(id) {
      this.isNew = false;
      this.visible = true;
      this.dataForm.id = id;
      this.$nextTick(() => {
        CategoryApi.getCategory({ id }).then(({ data }) => {
          this.dataForm = data;
        });
      });
    },
    dialogClose() {
      this.dataForm = {};
      this.$refs["dataForm"].resetFields();
    },
    // 表单提交
    submitData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.isNew) {
            CategoryApi.createCategory(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          } else {
            CategoryApi.updateCategory(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          }
        }
      });
    },
  },
};
</script>
